package com.project.running.web.Listener;

import com.project.running.Service.GraderecordService;
import com.project.running.Service.Impl.GraderecordServiceImpl;
import com.project.running.Service.Impl.MemberInfoServiceImpl;
import com.project.running.Service.Impl.PointrecordServiceImpl;
import com.project.running.Service.MemberInfoService;
import com.project.running.Service.PointrecordService;
import com.project.running.bean.Graderecord;
import com.project.running.bean.Memberinfo;
import com.project.running.bean.Pointrecord;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.List;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/6-10-06-20:11
 * @Description：com.project.running.web.Listener
 */

@WebListener
public class IndexListener implements HttpSessionAttributeListener {

    @Autowired
    GraderecordService service1 ;
    @Autowired
    PointrecordService service2;
    @Autowired
    MemberInfoService service3;

    @Override
    public void attributeAdded(HttpSessionBindingEvent he) {

        String name = he.getName();
        Graderecord graderecord = null;
        if(name.equals("memberInfo")){
            Memberinfo value = (Memberinfo) he.getValue();
            graderecord = service1.selectGradeByPoint(value.getPoint());
            he.getSession().setAttribute("graderecord",graderecord);
            System.out.println(graderecord);

            int visitNum = service2.getVisitNum();                      //记录访问次数
            he.getSession().setAttribute("visitNum",visitNum);

            List<Memberinfo> tenOfMember = service3.getTenOfMember();         //获取积分在前10名的成员
            he.getSession().setAttribute("tenofMember",tenOfMember);

            int onlineNum = service3.getOnlineNum();
            he.getSession().setAttribute("onlineNum",onlineNum);
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
